import io
from collections import deque
import Global, Parser


def printSemanticsStack():
    if Global.flag[12] == 1:
        Global.outputFile.write('		The semantics stack now: ')
        for i in range(len(Global.semantics_stack)):
            Global.outputFile.write(Global.semantics_stack[i]+' | ')
        Global.outputFile.write('\n')

def print4Tuple(s1, s2, s3, s4):
    s = 5*'\t'+'('+s1+', '+s2+', '+s3+', '+s4+')\n'
    if Global.flag[20] == 1:
        flag20Tuple = 5*'\t' + "(20, flag, #, #)\n"
        Global.outputFile.write(flag20Tuple)
        Global.tuples.append(['20', 'flag', '#', '#'])
    if Global.flag[13] == 1:
        Global.outputFile.write(s)
    Global.tuples.append([s1,s2,s3,s4])


def printSymbolTableEntry(name, type, shape, row, col, calltype ):
    if Global.flag[15] == 1:
        if Global.index == 0:
            Global.outputFile.write('		Global ST is: ST Entry [Name: '+name+' Type: '+type+' Shape: '+shape+' Rows: '+row+' Cols: '+col+']\n')
        elif calltype == '':
            Global.outputFile.write('		Local ST is: ST Entry [Name: '+name+' Type: '+type+' Shape: '+shape+' Rows: '+row+' Cols: '+col+']\n')
        else:
            Global.outputFile.write('		Local ST is: ST Entry [Name: '+name+' Type: '+type+' Shape: '+shape+' Rows: '+row+' Cols: '+col+' Calltype: '+calltype+']\n')

def printGlobalSymbolTable():
    if Global.flag[16] == 1:
        Global.outputFile.write('		Global ST is:\n')
        i = 0
        while i < len(Global.symbol_table[0]):
            name = Global.symbol_table[0][i]
            type = Global.symbol_table[0][i+1]
            row = Global.symbol_table[0][i+2]
            col = Global.symbol_table[0][i+3]
            shape = Global.symbol_table[0][i+4]
            Global.outputFile.write(14*' '+'ST Entry [Name: '+name+' Type: '+type+' Shape: '+shape+' Rows: '+row+' Cols: '+col+']\n')
            i = i+5

def printLocalSymbolTable():
    if Global.flag[14] == 1:
        Global.outputFile.write('		Local ST is:\n')
        i = 0
        while i < len(Global.symbol_table[1]):
            name = Global.symbol_table[1][i]
            type = Global.symbol_table[1][i+1]
            row = Global.symbol_table[1][i+2]
            col = Global.symbol_table[1][i+3]
            shape = Global.symbol_table[1][i+4]
            calltype = Global.symbol_table[1][i+5]
            if calltype == '':
                Global.outputFile.write(13*' '+'ST Entry [Name: '+name+' Type: '+type+' Shape: '+shape+' Rows: '+row+' Cols: '+col+']\n')
            else:
                Global.outputFile.write(13*' '+'ST Entry [Name: '+name+' Type: '+type+' Shape: '+shape+' Rows: '+row+' Cols: '+col+' Calltype: '+calltype+']\n')
            i = i+6

def variableDeclareError(var):
    if var in Global.symbol_table[Global.index]:
        Global.outputFile.write('	'+'\t'+'variable: '+var+' is doubly declared!\n')

def _insertSymbolTable(name, type, row, col, shape, calltype):
    variableDeclareError(name)
    Global.symbol_table[Global.index].append(name)
    Global.symbol_table[Global.index].append(type)
    Global.symbol_table[Global.index].append(row)
    Global.symbol_table[Global.index].append(col)
    Global.symbol_table[Global.index].append(shape)
    if Global.index == 1:
        Global.symbol_table[Global.index].append(calltype)
    printSymbolTableEntry(name, type, shape, row, col, calltype)

def searchSymbolTable(index, offset):
    if Global.semantics_stack[index] in Global.symbol_table[1]:
        return Global.symbol_table[1][Global.symbol_table[1].index(Global.semantics_stack[index])+offset]
    elif Global.semantics_stack[index] in Global.symbol_table[0]:
        return Global.symbol_table[0][Global.symbol_table[0].index(Global.semantics_stack[index])+offset]
    else :
        return -1

def searchSymbolTable2(var, offset):
    if var in Global.symbol_table[1]:
        return Global.symbol_table[1][Global.symbol_table[1].index(var)+offset]
    elif var in Global.symbol_table[0]:
        return Global.symbol_table[0][Global.symbol_table[0].index(var)+offset]
    else :
        return -1

def searchSTFromSemanticsStack(index, offset):
    return Global.symbol_table[Global.index][int(Global.semantics_stack[index])+offset]

def removeType(string):
    if string[1] == '$':
        return string
    else :
        return string[1:]

def reduction_7(index1, index2):
    name = Global.semantics_stack[index1]
    type = searchSTFromSemanticsStack(index2, 1)
    row = searchSTFromSemanticsStack(index2, 2)
    col = searchSTFromSemanticsStack(index2, 3)
    shape = searchSTFromSemanticsStack(index2, 4)
    _insertSymbolTable(name, type, row, col, shape, '')
    print4Tuple(name, 'MEMORY', row, col)
    return str(Global.symbol_table[Global.index].index(name))

def reduction_9(name, type, row, col, shape):
    _insertSymbolTable(name, type, row, col, shape, '')
    print4Tuple(name, 'MEMORY', row, col)
    return str(Global.symbol_table[Global.index].index(name))

def reduction_20():
    name = Global.semantics_stack[-1]
    type = 'procedure'
    row = '#'
    col = '#'
    shape = 'N/A'
    _insertSymbolTable(name, type, row, col, shape, '')
    Global.index = 1
    Global.symbol_table[1] = []
    _insertSymbolTable(name, type, row, col, shape, '')
    print4Tuple(name, 'PROCEDURE', row, col)
    return name

def reduction_24(name, type, row, col, shape, calltype):
    Global.index = 1
    _insertSymbolTable(name, type, row, col, shape, calltype)
    if calltype == 'reference':
		#This is FORMALRPARAMETER in pragmatics
        print4Tuple(name, 'FORMALRPARAMETER', row, col)
    else:
		#This is FORMALVPARAMETER in pragmatics
        print4Tuple(name, 'FORMALVPARAMETER', row, col)

def reduction_40(var, length, shape):
    if searchSymbolTable2(var, 1) == -1:
        Global.outputFile.write('\tthe variable: '+var+' is not declared\n')        
    elif searchSymbolTable2(var, 4) != shape:
        Global.outputFile.write('\tThe type of the variable: '+var+' is not '+shape+'\n')
    if searchSymbolTable2(var, 1) == 'integer':
        if length == '#':
            print4Tuple('#', 'IACTUALVPARAMETER', var, length)
        else :
            print4Tuple('#', 'IACTUALVSUBPARAMETER', var, length)
    else :
        if length == '#':
            print4Tuple('#', 'RACTUALVPARAMETER', var, length)
        else :
            print4Tuple('#', 'RACTUALVSUBPARAMETER', var, length)		

def reduction_42(var, length, shape):
    if searchSymbolTable2(var, 1) == -1:
        Global.outputFile.write('\tthe variable: '+var+' is not declared\n')        
    elif searchSymbolTable2(var, 4) != shape:
        Global.outputFile.write('\tThe type of the variable: '+var+' is not '+shape+'\n') 
    if searchSymbolTable2(var, 1) == 'integer':
        if length == '#':
            print4Tuple('#', 'IACTUALVPARAMETER', var, length)
        else :
            print4Tuple('#', 'IACTUALVSUBPARAMETER', var, length)
    else :
        if length == '#':
            print4Tuple('#', 'RACTUALVPARAMETER', var, length)
        else :
            print4Tuple('#', 'RACTUALVSUBPARAMETER', var, length)
		
def reduction_42R(var, length, shape):
    if searchSymbolTable2(var, 1) == -1:
        Global.outputFile.write('\tthe variable: '+var+' is not declared\n')        
    elif searchSymbolTable2(var, 4) != shape:
        Global.outputFile.write('\tThe type of the variable: '+var+' is not '+shape+'\n') 
	if searchSymbolTable2(var, 1) == 'integer':
            if length == '#':
               print4Tuple('#', 'IACTUALRPARAMETER', var, length)
            else :
                print4Tuple('#', 'IACTUALRSUBPARAMETER', var, length)
    else :
        if length == '#':
            print4Tuple('#', 'RACTUALRPARAMETER', var, length)
        else :
            print4Tuple('#', 'RACTUALRSUBPARAMETER', var, length)


def reduction_56(var, calltype, length, shape):
    if searchSymbolTable2(var, 1) == -1:
        Global.outputFile.write('\tthe variable: '+var+' is not declared\n')        
    elif searchSymbolTable2(var, 4) != shape:
        Global.outputFile.write('\tThe type of the variable: '+var+' is not '+shape+'\n')     
    if searchSymbolTable2(var, 1) == 'integer':
        if length == '#':
            if calltype == 'value':
                print4Tuple('#', 'IACTUALVPARAMETER', var, length)
            if calltype == 'reference':
                print4Tuple('#', 'IACTUALRPARAMETER', var,  length)
        else :
            if calltype == 'value':
                print4Tuple('#', 'IACTUALVSUBPARAMETER', var, length)
            if calltype == 'reference':
                print4Tuple('#', 'IACTUALRSUBPARAMETER', var,  length)
    else :
        if length == '#':
            if calltype == 'value':
                print4Tuple('#', 'RACTUALVPARAMETER', var, length)
            if calltype == 'reference':
                print4Tuple('#', 'RACTUALRPARAMETER', var,  length)
        else :
            if calltype == 'value':
                print4Tuple('#', 'RACTUALVSUBPARAMETER', var, length)
            if calltype == 'reference':
                print4Tuple('#', 'RACTUALRSUBPARAMETER', var,  length)
                
def reduction_59():
    a = 'L$'+str(Global.L_m)
    print4Tuple(a, 'LABEL', '#', '#')    
    Global.L_m += 1
    return a
		
def reduction_69(string, expr1, expr2):
    a = 'B$'+str(Global.T_m)
    print4Tuple(a, string, removeType(expr1), removeType(expr2))
    Global.T_m += 1
    return a
	
def reduction_75(string):
    a = 'B$'+str(Global.T_m)
    if Global.semantics_stack[-3][0] == 'I':
        print4Tuple(a, 'I'+string, removeType(Global.semantics_stack[-3]), removeType(Global.semantics_stack[-1]))
    else : 
        print4Tuple(a, 'R'+string, removeType(Global.semantics_stack[-3]), removeType(Global.semantics_stack[-1]))
    Global.T_m += 1 
    return a
	
def reduction_82(string):
        
    a = Global.semantics_stack[-3][0]
    b = Global.semantics_stack[-1][0]
    if a == 'I' and b == 'I':
        c = 'I$'+ str(Global.T_m)
        print4Tuple(c, 'I'+string, removeType(Global.semantics_stack[-3]), removeType(Global.semantics_stack[-1]))
        Global.T_m += 1
        return c
    elif a == 'R' and b == 'R':
        c = 'R$'+ str(Global.T_m)
        print4Tuple(c, 'R'+string, removeType(Global.semantics_stack[-3]), removeType(Global.semantics_stack[-1]))
        Global.T_m += 1
        return c
    elif a == 'I' and b == 'R':
        d = 'R$'+str(Global.T_m)
        Global.T_m += 1
        print4Tuple(d, 'CONVERTITOR', removeType(Global.semantics_stack[-3]), '#')
        c = 'R$'+ str(Global.T_m)
        print4Tuple(c, 'R'+string, d, removeType(Global.semantics_stack[-1]))
        Global.T_m += 1
        return c
    elif a == 'R' and b == 'I':
        d = 'R$'+str(Global.T_m)
        Global.T_m += 1
        print4Tuple(d, 'CONVERTITOR', removeType(Global.semantics_stack[-1]), '#')
        c = 'R$'+ str(Global.T_m)
        print4Tuple(c, 'R'+string,  removeType(Global.semantics_stack[-3]), d)
        Global.T_m += 1
        return c
		
def reduction_84():
    if Global.semantics_stack[-1][0] == 'I':
        c = 'I$'+ str(Global.T_m)
        print4Tuple(c, 'ISUB', '0', removeType(Global.semantics_stack[-1]))
    else :
        c = 'R$'+ str(Global.T_m)
        print4Tuple(c, 'RSUB', '0', removeType(Global.semantics_stack[-1])) 
    Global.T_m += 1
    return c
		
def insertSymbolTable(reductions, reduction):
    #print "reduction " + str(reductions.index(reduction) + 1   
    value = ''
    if reductions.index(reduction) == 0:
        print4Tuple(Global.semantics_stack[-3], 'ENDPROGRAM', '#', '#')
    elif reductions.index(reduction) == 1:
        print4Tuple('#', 'BEGINPROGRAM', Global.semantics_stack[-1], '#')
        value = Global.semantics_stack[-1]
    elif reductions.index(reduction) == 4:
        print4Tuple('#', 'ENDDECLARATIONS', '#', '#')
    elif reductions.index(reduction) == 7:
        value = reduction_9(Global.semantics_stack[-1], Global.semantics_stack[-2], '1', '#', 'scalar')
    elif reductions.index(reduction) == 8:
        value = reduction_9(Global.semantics_stack[-2], Global.semantics_stack[-3], Global.semantics_stack[-1], '#', 'vector')
    elif reductions.index(reduction) == 9:
        value = reduction_9(Global.semantics_stack[-4], Global.semantics_stack[-5], Global.semantics_stack[-3], Global.semantics_stack[-1], 'matrix')
    elif reductions.index(reduction) == 10:
        value = 'integer'
    elif reductions.index(reduction) == 11:
        value = 'real'
    elif reductions.index(reduction) == 15:
	Global.index = 0
        print4Tuple(Global.semantics_stack[-4], 'ENDPROCEDURE', '#', '#')
        printLocalSymbolTable()
    elif reductions.index(reduction) == 16:
	Global.index = 0
        print4Tuple(Global.semantics_stack[-3], 'ENDPROCEDURE', '#', '#')
        printLocalSymbolTable()
    elif reductions.index(reduction) == 17:
        print4Tuple('#', 'ENDFORMALPARAMETERLIST', '#', '#')
        value = Global.semantics_stack[-3]
    elif reductions.index(reduction) == 18:
		#print4Tuple('#', 'NOFORMALPARAMETERS', '#', '#')
		value = Global.semantics_stack[-2]
    elif reductions.index(reduction) == 19:
        value = reduction_20()
    elif reductions.index(reduction) == 20:
        reduction_24(Global.semantics_stack[-1], Global.semantics_stack[-2], '1', '#', 'scalar', Global.semantics_stack[-3])
    elif reductions.index(reduction) == 21:
        reduction_24(Global.semantics_stack[-2], Global.semantics_stack[-3], Global.semantics_stack[-1], '#', 'vector', Global.semantics_stack[-4])
    elif reductions.index(reduction) == 22:
        reduction_24(Global.semantics_stack[-4], Global.semantics_stack[-5], Global.semantics_stack[-3], Global.semantics_stack[-1], 'matrix', Global.semantics_stack[-6])
    elif reductions.index(reduction) == 23:
        print4Tuple('#', 'BEGINFORMALPARAMETERLIST', '#', '#')
        reduction_24(Global.semantics_stack[-1], Global.semantics_stack[-2], '1', '#', 'scalar', Global.semantics_stack[-3])
    elif reductions.index(reduction) == 24:
        print4Tuple('#', 'BEGINFORMALPARAMETERLIST', '#', '#')
        reduction_24(Global.semantics_stack[-2], Global.semantics_stack[-3], Global.semantics_stack[-1], '#', 'vector', Global.semantics_stack[-4])
    elif reductions.index(reduction) == 25:
        print4Tuple('#', 'BEGINFORMALPARAMETERLIST', '#', '#')
        reduction_24(Global.semantics_stack[-4], Global.semantics_stack[-5], Global.semantics_stack[-3], Global.semantics_stack[-1], 'matrix', Global.semantics_stack[-6])
    elif reductions.index(reduction) == 26:
        value = 'value'
    elif reductions.index(reduction) == 27:
        value = 'reference'
    elif reductions.index(reduction) == 28:
	print4Tuple('#', 'ENDMAIN', '#', '#')
    elif reductions.index(reduction) == 29:
        print4Tuple('LABEL', 'MAIN', '#', '#')
    elif reductions.index(reduction) == 35:
		#This is different
        #print4Tuple('#', 'ENDOFINPUTPARAMETERS', '#', '#')
        #print4Tuple('scanf', 'ENDINPUTPARAMETERS', '#', '#')
		print4Tuple('SCANF', 'ENDPROCEDURECALL', '#', '#')
    elif reductions.index(reduction) == 36:
		#This is different
        #print4Tuple('printf', 'ENDOUTPUTPARAMETERS', '#', '#')
		print4Tuple('PRINTF', 'ENDPROCEDURECALL', '#', '#')
    elif reductions.index(reduction) == 38:
        #print4Tuple('#', 'INPUTPARAMETER', Global.semantics_stack[-1], '#')
		reduction_42(Global.semantics_stack[-1], '#', 'scalar')
    elif reductions.index(reduction) == 39:
        if Global.semantics_stack[-2][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-2]+' not an integer expression\n')
        reduction_40(Global.semantics_stack[-4], removeType(Global.semantics_stack[-2]), 'vector')
        print4Tuple('#', 'INPUTSUBPARAMETER', Global.semantics_stack[-4], Global.semantics_stack[-2])
    elif reductions.index(reduction) == 40:
        if Global.semantics_stack[-4][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-4]+' not an integer expression\n')
        if Global.semantics_stack[-2][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-2]+' not an integer expression\n')            
        a = 'I$'+str(Global.T_m)
        print4Tuple(a, 'IMULT', removeType(Global.semantics_stack[-4]), Global.symbol_table[Global.index][Global.symbol_table[Global.index].index(Global.semantics_stack[-6])+3])    
        Global.T_m += 1
        b = 'I$'+str(Global.T_m)
        print4Tuple(b, 'IADD', a, removeType(Global.semantics_stack[-2]))    
        reduction_40(Global.semantics_stack[-6], b, 'matrix')
        print4Tuple('#', 'INPUTSUBPARAMETER', Global.semantics_stack[-6], b)
        Global.T_m += 1
    elif reductions.index(reduction) == 41:
        #print4Tuple('#', 'CALL', 'scanf', '#')    
        #print4Tuple('#', 'INPUTPARAMETER', Global.semantics_stack[-1], '#')
	print4Tuple('#', 'PROCEDURECALL', 'SCANF', '#')    
        print4Tuple('#', 'BEGINACTUALPARAMETERLIST', '#', '#')    
        print4Tuple('#', 'SACTUALVPARAMETER', Global.semantics_stack[-1], '#')
    elif reductions.index(reduction) == 42:
	reduction_40(Global.semantics_stack[-1], '#', 'scalar')
    elif reductions.index(reduction) == 43:
        #print4Tuple('#', 'OUTPUTPARAMETER', Global.semantics_stack[-1], '#')
	reduction_40(Global.semantics_stack[-1], '#', 'scalar')
    elif reductions.index(reduction) == 44:
        reduction_40(Global.semantics_stack[-4], removeType(Global.semantics_stack[-2]), 'vector')
        #print4Tuple('#', 'OUTPUTSUBPARAMETER', Global.semantics_stack[-4], Global.semantics_stack[-2])
    elif reductions.index(reduction) == 45:
        a = 'I$'+str(Global.T_m)
        print4Tuple(a, 'IMULT', removeType(Global.semantics_stack[-4]), searchSymbolTable(-6, 3))    
        Global.T_m += 1
        b = 'I$'+str(Global.T_m)
        print4Tuple(b, 'IADD', a, removeType(Global.semantics_stack[-2]))    
        reduction_40(Global.semantics_stack[-6], b, 'matrix')
        Global.T_m += 1
        #print4Tuple('#', 'OUTPUTSUBPARAMETER', Global.semantics_stack[-6], b)
    elif reductions.index(reduction) == 46:
        #print4Tuple('#', 'CALL', 'printf', '#')
        #print4Tuple('#', 'OUTPUTPARAMETER', Global.semantics_stack[-1], '#')
        print4Tuple('#', 'PROCEDURECALL', 'PRINTF', '#')   
	print4Tuple('#', 'SACTUALVPARAMETER', Global.semantics_stack[-1], '#')
	#print4Tuple('#', 'BEGINACTUALPARAMETERLIST', '#', '#')  		
        #print4Tuple('#', 'SACTUALVPARAMETER', Global.semantics_stack[-1], '#')   
    elif reductions.index(reduction) == 47:
        #print4Tuple(Global.semantics_stack[-3], 'ENDACTUALPARAMETERLIST', '#', '#')
	print4Tuple(Global.semantics_stack[-3], 'ENDPROCEDURECALL', '#', '#')
    elif reductions.index(reduction) == 48:
        print4Tuple('#', 'NOACTUALPARAMETERS', '#', '#')
	print4Tuple(Global.semantics_stack[-1], 'ENDPROCEDURECALL', '#', '#')
    elif reductions.index(reduction) == 49:
        if searchSymbolTable2(Global.semantics_stack[-1], 1) != 'procedure':
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-1]+' is not a procedure\n') 
        print4Tuple(Global.semantics_stack[-1], 'PROCEDURECALL', '#', '#')
        value = Global.semantics_stack[-1]
    elif reductions.index(reduction) == 50:
        reduction_56(Global.semantics_stack[-1], Global.semantics_stack[-2], '#', 'scalar')
    elif reductions.index(reduction) == 51:
        reduction_56(Global.semantics_stack[-4], Global.semantics_stack[-5], removeType(Global.semantics_stack[-2]), 'vector')
    elif reductions.index(reduction) == 52:
        a = 'I$'+str(Global.T_m)
        print4Tuple(a, 'IMULT', removeType(Global.semantics_stack[-4]), searchSymbolTable(-6, 3))    
        Global.T_m += 1
        b = 'I$'+str(Global.T_m)
        print4Tuple(b, 'IADD', a, removeType(Global.semantics_stack[-2]))    
        reduction_56(Global.semantics_stack[-6], Global.semantics_stack[-7], b, 'matrix')
        Global.T_m += 1
    elif reductions.index(reduction) == 53:
        print4Tuple('#', 'BEGINACTUALPARAMETERLIST', '#', '#')
        reduction_56(Global.semantics_stack[-1], Global.semantics_stack[-2], '#', 'scalar')
    elif reductions.index(reduction) == 54:
        print4Tuple('#', 'BEGINACTUALPARAMETERLIST', '#', '#')
        reduction_56(Global.semantics_stack[-4], Global.semantics_stack[-5], removeType(Global.semantics_stack[-2]), 'vector')
    elif reductions.index(reduction) == 55:
        print4Tuple('#', 'BEGINACTUALPARAMETERLIST', '#', '#')
        a = 'I$'+str(Global.T_m)
        print4Tuple(a, 'IMULT', removeType(Global.semantics_stack[-4]), searchSymbolTable(-6, 3))    
        Global.T_m += 1
        b = 'I$'+str(Global.T_m)
        print4Tuple(b, 'IADD', a, removeType(Global.semantics_stack[-2]))    
        reduction_56(Global.semantics_stack[-6], Global.semantics_stack[-7], b, 'matrix')
        Global.T_m += 1
    elif reductions.index(reduction) == 56:
        value = reduction_59()
    elif reductions.index(reduction) == 57:
        value = reduction_59()
    elif reductions.index(reduction) == 58:
        print4Tuple('L$'+str(Global.L_m+1), 'JUMP', '#', '#')
        value = reduction_59()
    elif reductions.index(reduction) == 59:
        if Global.semantics_stack[-3][0] != 'B':
            Global.outputFile.write('Non-Boolean expression in if\n')
        print4Tuple('L$'+str(Global.L_m), 'CJUMP', Global.semantics_stack[-3], '#')
    elif reductions.index(reduction) == 60:
        print4Tuple(Global.semantics_stack[-3], 'JUMP', '#', '#')
        label1 = Global.semantics_stack[-3]
        label2 = 'L$' + str(int(label1[label1.index('$') + 1:]) + 1)
        print4Tuple(label2, 'LABEL', '#', '#')
    elif reductions.index(reduction) == 61:
        if Global.semantics_stack[-3][0] != 'B':
            Global.outputFile.write('Non-Boolean expression in While\n')
        label1 = Global.semantics_stack[-5]
        label2 = 'L$' + str(int(label1[label1.index('$') + 1:]) + 1)
        print4Tuple(label2, 'CJUMP', Global.semantics_stack[-3], '#')
        value = label1;
    elif reductions.index(reduction) == 62:
        a = 'L$' + str(Global.L_m)
        Global.L_m+=1
        b = 'L$' + str(Global.L_m)
        Global.L_m+=1
        print4Tuple(a, 'LABEL', '#', '#')
        value = a;
        

    elif reductions.index(reduction) == 63:
        if searchSymbolTable2(Global.semantics_stack[-3], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-3]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-3], 4) != 'scalar':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-3]+' is not scalar\n')     
        print4Tuple(Global.semantics_stack[-3], 'STORE', removeType(Global.semantics_stack[-1]), '#')
    elif reductions.index(reduction) == 64:
        if searchSymbolTable2(Global.semantics_stack[-6], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-6]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-6], 4) != 'vector':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-6]+' is not vector\n')
        if Global.semantics_stack[-4][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-4]+' not an integer expression\n')
        print4Tuple(Global.semantics_stack[-6], 'SUBSTORE', removeType(Global.semantics_stack[-1]), removeType(Global.semantics_stack[-4]))
    elif reductions.index(reduction) == 65:
        if searchSymbolTable2(Global.semantics_stack[-8], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-8]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-8], 4) != 'matrix':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-8]+' is not matrix\n')
        if Global.semantics_stack[-6][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-6]+' not an integer expression\n')
        if Global.semantics_stack[-4][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-4]+' not an integer expression\n')            
        a = 'I$'+str(Global.T_m)
        print4Tuple(a, 'IMULT', removeType(Global.semantics_stack[-6]), searchSymbolTable(-8, 3))    
        Global.T_m += 1
        b = 'I$'+str(Global.T_m)
        print4Tuple(b, 'IADD', a, removeType(Global.semantics_stack[-4]))    
        print4Tuple(Global.semantics_stack[-8], 'SUBSTORE', removeType(Global.semantics_stack[-1]), b)
        Global.T_m += 1

##To be confirmed

    elif reductions.index(reduction) == 66:
        if searchSymbolTable2(Global.semantics_stack[-4], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-4]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-4], 4) != 'scalar':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-4]+' is not scalar\n')     
        print4Tuple(Global.semantics_stack[-4], 'STORE', removeType(Global.semantics_stack[-2]), '#')
    elif reductions.index(reduction) == 67:
        if searchSymbolTable2(Global.semantics_stack[-7], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-7]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-7], 4) != 'vector':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-7]+' is not vector\n')
        if Global.semantics_stack[-5][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-5]+' not an integer expression\n')
        print4Tuple(Global.semantics_stack[-7], 'SUBSTORE', removeType(Global.semantics_stack[-2]), removeType(Global.semantics_stack[-5]))
    elif reductions.index(reduction) == 68:
        if searchSymbolTable2(Global.semantics_stack[-9], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-9]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-9], 4) != 'matrix':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-9]+' is not matrix\n')
        if Global.semantics_stack[-7][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-7]+' not an integer expression\n')
        if Global.semantics_stack[-5][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-5]+' not an integer expression\n')            
        a = 'I$'+str(Global.T_m)
        print4Tuple(a, 'IMULT', removeType(Global.semantics_stack[-7]), searchSymbolTable(-9, 3))    
        Global.T_m += 1
        b = 'I$'+str(Global.T_m)
        print4Tuple(b, 'IADD', a, removeType(Global.semantics_stack[-5]))    
        print4Tuple(Global.semantics_stack[-9], 'SUBSTORE', removeType(Global.semantics_stack[-2]), b)
        Global.T_m += 1
		
		
    elif reductions.index(reduction) == 69:
        value = reduction_69('OR', Global.semantics_stack[-3], Global.semantics_stack[-1])       
    elif reductions.index(reduction) == 70:
        value = Global.semantics_stack[-1]
    elif reductions.index(reduction) == 71:
        value = reduction_69('AND', Global.semantics_stack[-3], Global.semantics_stack[-1])
    elif reductions.index(reduction) == 72:
        value = Global.semantics_stack[-1]  	
    elif reductions.index(reduction) == 73:
        value = reduction_69('NOT', Global.semantics_stack[-1], '#')
    elif reductions.index(reduction) == 74:
        value = Global.semantics_stack[-1]
	
    elif reductions.index(reduction) == 75:
        value = reduction_75('LESSTHAN')
    elif reductions.index(reduction) == 76:
        value = reduction_75('LESSEQUAL')
    elif reductions.index(reduction) == 77:
        value = reduction_75('GREATERTHAN')
    elif reductions.index(reduction) == 78:
        value = reduction_75('GREATEREQUAL')
    elif reductions.index(reduction) == 79:
        value = reduction_75('EQUAL')
    elif reductions.index(reduction) == 80:
        value = reduction_75('NOTEQUAL')
    elif reductions.index(reduction) == 81:
        value = Global.semantics_stack[-1]       
    elif reductions.index(reduction) == 82:
        value = reduction_82('ADD')
    elif reductions.index(reduction) == 83:
        value = reduction_82('SUB')
    elif reductions.index(reduction) == 84:
        value = reduction_84()
    elif reductions.index(reduction) == 85:
        value = Global.semantics_stack[-1]
    elif reductions.index(reduction) == 86:
        value = reduction_82('MULT')
    elif reductions.index(reduction) == 87:
        value = reduction_82('DIV')
    elif reductions.index(reduction) == 88:
        value = Global.semantics_stack[-1]
    elif reductions.index(reduction) == 89:
        value = Global.semantics_stack[-2]
    elif reductions.index(reduction) == 90:
        if searchSymbolTable2(Global.semantics_stack[-6], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-6]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-6], 4) != 'matrix':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-6]+' is not matrix\n')
        if Global.semantics_stack[-4][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-4]+' not an integer expression\n')
        if Global.semantics_stack[-2][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-2]+' not an integer expression\n')            
        a = 'I$'+str(Global.T_m)
        print4Tuple(a, 'IMULT', removeType(Global.semantics_stack[-4]), searchSymbolTable(-6, 3))
        Global.T_m += 1
        b = 'I$'+str(Global.T_m)
        print4Tuple(b, 'IADD', a, removeType(Global.semantics_stack[-2]))    
        Global.T_m += 1
        if  searchSymbolTable(-6, 1) == 'integer':
            value = 'I$'+ str(Global.T_m)
        else : 
            value = 'R$'+ str(Global.T_m)
        print4Tuple(value, 'SUBLOAD', Global.semantics_stack[-6], b)
        Global.T_m += 1
    elif reductions.index(reduction) == 91:
        if searchSymbolTable2(Global.semantics_stack[-4], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-4]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-4], 4) != 'vector':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-4]+' is not vector\n')
        if Global.semantics_stack[-2][0] != 'I':
            Global.outputFile.write('\tsubscript: '+Global.semantics_stack[-2]+' not an integer expression\n')            
        if  searchSymbolTable(-4, 1) == 'integer':
            value = 'I$'+ str(Global.T_m)
        else :
            value = 'R$'+ str(Global.T_m)
        print4Tuple(value, 'SUBLOAD', Global.semantics_stack[-4], removeType(Global.semantics_stack[-2]))
        Global.T_m += 1
    
    elif reductions.index(reduction) == 92:
        if searchSymbolTable2(Global.semantics_stack[-1], 1) == -1:
            Global.outputFile.write('\tthe variable: '+Global.semantics_stack[-1]+' is not declared\n')        
        elif searchSymbolTable2(Global.semantics_stack[-1], 4) != 'scalar':
            Global.outputFile.write('\tThe type of the variable: '+Global.semantics_stack[-1]+' is not scalar\n')        
        if searchSymbolTable(-1, 1) == 'integer':
            value = 'I'+Global.semantics_stack[-1]
        else :
            value = 'R'+Global.semantics_stack[-1]
    elif reductions.index(reduction) == 93:
        value = Global.semantics_stack[-1]
    elif reductions.index(reduction) == 94:
        value = 'I'+Global.semantics_stack[-1]
    elif reductions.index(reduction) == 95:
        value = 'R'+Global.semantics_stack[-1]
    
		
    else :
        value = '#'
    if value == '':
        value = '#'
    return value
